﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace PloobsEngine.SceneControl
{
    public delegate void VideoEnded();

    public  class MovieScreen : IScreen
    {

        public MovieScreen(String location)
        {
            this.location = location;
        }

        protected string location;
        protected Video myVideoFile;
        protected VideoPlayer videoPlayer;
        protected EngineStuff engine;
        protected bool started = false;
        protected bool play = true;
        protected SpriteBatch sprite;
        protected VideoEnded ve;

        public VideoEnded VideoEndedDelegate
        {
            set
            {
                this.ve += value;
            }
        }

        public override void LoadContent(EngineStuff es)
        {
            this.engine = es;
             videoPlayer = new VideoPlayer();
             myVideoFile = es.Content.Load<Video>(location);
             videoPlayer.IsLooped = false;
             sprite= new SpriteBatch(EngineStuff.GraphicsDevice);
             base.LoadContent(es);

        }
        public bool IsLooped
        {
            get
            {
                return videoPlayer.IsLooped;
            }
            set
            {
                videoPlayer.IsLooped = value;
            }

        }

        public void SetSize(int height, int width)
        {
            Viewport vp = EngineStuff.GraphicsDevice.Viewport;
            vp.Width = width;
            vp.Height = height;            
        }


        public void Play()
        {
            if (started)
                videoPlayer.Play(myVideoFile);
            else
                play = true;

        }
        public void Stop()
        {
            if (started)
                videoPlayer.Stop();
            else
                play = false;
        }
        public void Resume()
        {
            if (started)
                videoPlayer.Resume();
        }
        public void setVolume(float vol)
        {
            if (started)
            videoPlayer.Volume = vol;
            throw new Exception("Not initiated yet");
        }
        public MediaState getState()
        {
            if (started)
            return videoPlayer.State;
            throw new Exception("Not initiated yet");
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            started = true;
            if(play)
            {            
                videoPlayer.Play(myVideoFile);
                play = false;
             
            }

            if (videoPlayer.State == MediaState.Stopped)
            {
                if (ve != null)
                    ve();
                VideoEnded();
            }

            this.UpdateMovie(gameTime);
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        public virtual void VideoEnded()
        {
        }


        public virtual void UpdateMovie(GameTime gt)
        {
        }

        public override void  Draw(GameTime gameTime)
        {
            EngineStuff.GraphicsDevice.Clear(Color.Black);
            Texture2D videoTexture = null;
           if (videoPlayer.State != MediaState.Stopped)
               videoTexture = videoPlayer.GetTexture();

           Rectangle screen = new Rectangle(EngineStuff.GraphicsDevice.Viewport.X,
                EngineStuff.GraphicsDevice.Viewport.Y,
                EngineStuff.GraphicsDevice.Viewport.Width,
                EngineStuff.GraphicsDevice.Viewport.Height);

            // Draw the video, if we have a texture to draw.
            if (videoTexture != null)
            {
                sprite.Begin(SpriteBlendMode.None,SpriteSortMode.Immediate,SaveStateMode.SaveState);
                sprite.Draw(videoTexture, screen, Color.White);
                sprite.End();
            }



            base.Draw(gameTime);
        }



    }
}
